package com.flight.dao;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

public class BaseDao {

    //1.    定义需要的工具类对象
    protected Connection connection = null;
    protected PreparedStatement pps = null;
    protected ResultSet resultSet = null;
    protected int k = 0;
    private static String url;
    private static String userName;
    private static String userPass;
    private static String dirverName;
    //德鲁伊
    private static DruidDataSource dds = new DruidDataSource();

    //2.    加载驱动
    static {
        ResourceBundle db = ResourceBundle.getBundle("db");
        url = db.getString("url");
        userName = db.getString("user");
        userPass = db.getString("password");
        dirverName = db.getString("driver");
        dds.setUrl(url);
        dds.setUsername(userName);
        dds.setPassword(userPass);
        dds.setDriverClassName(dirverName);
    }

    //3.    获得连接
    protected Connection getConnection() {
        //德鲁伊
        try {
            connection = dds.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    //4.    创建通道
    protected PreparedStatement getPps(String sql){
        try {
            getConnection();
            pps = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return pps;
    }

    //5.    给占位符赋值 list中保存的是占位符所赋的值
    private void setParams(List list){
        try {
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    pps.setObject(i + 1, list.get(i));
                }
            }
        }
         catch (SQLException throwables) {
             throwables.printStackTrace();
         }
    }

    //6.    增删改调取的方法
    protected int update(String sql,List params){
        try {
            getPps(sql);
            setParams(params);
            k = pps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return this.k;
    }

    //7.    查询的时候调取一个方法
    protected ResultSet query(String sql,List list){
        try {
            getPps(sql);
            setParams(list);
            resultSet = pps.executeQuery();
            return resultSet;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //8.    关闭资源
    protected void closeAll(){
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (pps != null) {
                pps.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
